package com.py.framework.activiti.rest;

import java.sql.Timestamp;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;

import com.py.framework.activiti.entity.WorkFlowBus;
import com.py.framework.activiti.service.WorkflowService;
import com.py.framework.common.SpringContextHolder;

/**
 * 自定义执行监听器
 * @author Leegern
 * @date   2018年6月13日
 */
public class MyExecutionListener implements ExecutionListener {
	
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 8938453810291560933L;
	
	/**
	 * 结束事件，通过节点的名称，默认是End
	 */
	public static final String passEventName = "pass";
	
	/** 工作流Service **/
	private WorkflowService wfService;
	
	
	/*
	 * (non-Javadoc)
	 * @see org.activiti.engine.delegate.ExecutionListener#notify(org.activiti.engine.delegate.DelegateExecution)
	 */
	@Override
	public void notify(DelegateExecution execution) throws Exception {
		
		String eventName = execution.getEventName();
	
		// 监听结束事件
		if (EVENTNAME_END.equals(eventName)) {
			// 只能使用 ActivityName 才可以获取
			String activityName = execution.getCurrentActivityName();
			String procId = execution.getProcessInstanceId();
			
			WorkFlowBus entity = new WorkFlowBus();
			entity.setProcId(procId);
			// 工作流状态为关闭
			entity.setActStatus(2);
			// 处理状态为已处理
			entity.setDealStatus(2);
			// 如果是指定的字符则表示流程状态为审核通过
			if (passEventName.equals(activityName)) {
        		entity.setProStatus(3);
			} else {
				entity.setProStatus(3);
			}
			entity.setFinishTime(new Timestamp(System.currentTimeMillis()));
			
			if (null == wfService) {
				wfService = SpringContextHolder.getBean(WorkflowService.class);
				// 更新工作流状态
				wfService.updateWorkFlowBus(entity);
			}
		}		
	}
}